import uniDataSelect from "@/uni_modules/uni-data-select/components/uni-data-select/uni-data-select.vue";
import uInput from "@/node_modules/uview-plus/components/u-input/u-input.vue";
import uTextarea from '@/node_modules/uview-plus/components/u--textarea/u--textarea.vue'
import uRate from '@/node_modules/uview-plus/components/u-rate/u-rate.vue'
import uUpload from '@/node_modules/uview-plus/components/u-upload/u-upload.vue'

interface Type {
    value: number
    text: string
}

interface FormMethods {
    selectItem: (val: any) => void
    handleDeleteImage: (event: any) => void
    handleImageUpload: (event: any) => Promise<void>
}

interface FormData {
    complexity: number
    describe?: string
    foods: Array<{ name?: string; unit?: string }>
    imgs: any[]
    needMinutes: number
    steps: string[]
    title?: string
    type: number
}

export const getFormConfig = (formData: FormData, methods: FormMethods) => {
    const typeList: Type[] = [
        { value: 1, text: '菜单' },
        { value: 2, text: '其他' }
    ]

    return [
        {
            component: uniDataSelect,
            attrs: {
                localdata: typeList,
                placeholder: '请选择'
            },
            events: {
                change: methods.selectItem
            },
            label: '发布类型',
            prop: 'type'
        },
        {
            component: uInput,
            attrs: {
                placeholder: '请输入标题'
            },
            events: {},
            label: '标题',
            prop: 'title'
        },
        {
            component: uTextarea,
            attrs: {
                placeholder: '菜品描述',
                autoHeight: true,
                count: true,
                maxlength: 840
            },
            events: {},
            label: '描述',
            prop: 'describe'
        },
        {
            component: uInput,
            attrs: {
                placeholder: '制作时长',
                type: 'number'
            },
            events: {},
            label: '所需时间',
            prop: 'needMinutes'
        },
        {
            component: uRate,
            attrs: {
                count: 5,
                activeColor: '#ff6d70'
            },
            events: {},
            label: '制作难度',
            prop: 'complexity'
        },
        {
            component: uUpload,
            attrs: {
                multiple: true,
                maxCount: 9,
                fileList: formData.imgs,
                previewFullImage: true
            },
            events: {
                delete: methods.handleDeleteImage,
                afterRead: methods.handleImageUpload
            },
            label: '成品图片',
            prop: 'imgs'
        }
    ]
}

// @ts-ignore
export const uploadFile = (url: string): Promise<string> => {
    // @ts-ignore
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: 'http://192.168.2.21:7001/upload',
            filePath: url,
            name: 'file',
            formData: { user: 'test' },
            success: (res: any) => {
                // @ts-ignore
                setTimeout(() => {
                    resolve(res.data.data);
                }, 1000);
            },
            fail: (err:any) => {
                reject(err);
            }
        });
    });
};